<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>更新区县</title>
    <link rel="stylesheet" href="/static/common/layui/css/layui.css" media="all">
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=QIw7CEn9awTfvoOMeBoRfA5X4gHVYIB3"></script>
    <style type="text/css">
        .layui-upload-img {
            width: 92px;
            height: 92px;
            margin: 0 10px 10px 0;
        }

        .site-block {
            width: 600px;
            padding: 20px;
            border: 2px solid #eee;
        }

        .site-text {
            position: relative;
        }

        .title-location {
            font-size: 20px;
            font-weight: 600;
        }

        .rightlayer {
            display: inline-block;
        }
    </style>
</head>
<body style="margin: 20px 20px ">
<div class="site-text site-block ">
    <form class="layui-form layui-form-pane " action="">
        <div class="layui-form-item">
            <label class="title-location">更改商圈</label>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">选择区域</label>
            <div class="layui-input-block">
                <select name="parent_id" id="parent_id" lay-verify="">
                    <option value="">请选择所属区域</option>
                <#list data as obj>
                    <option value="${obj.county_id}" <#if obj.county_id == object.parent_id>selected="selected"</#if>>${obj.name}</option>
                </#list>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">商圈昵称</label>
            <div class="layui-input-block">
                <input type="text" name="name" id="cityName" value="${object.name}" required="" maxlength="8" onkeyup="theLocation()" lay-verify="required" placeholder="请输入商圈昵称(8字内)" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item" style="height: 199px!important;" pane>
            <label class="layui-form-label">坐标</label>
            <div class="layui-input-block">
                <div style="width:250px;height:180px;border:#ccc solid 1px;font-size:12px;margin: 4px 4px;display:inline-block;" id="allmap"></div>
                <div class="rightlayer">
                    <input id="cityName-coordinate" value="${object.name}" type="text" style="width:138px; margin-right:10px;display:inline-block;" disabled class="layui-input"/>
                    <button type="button" class="layui-btn" onclick="resetMapCenter()">获取</button>
                    <p> 坐标: &nbsp;<span id="coordinateShow"></span>
                </div>
                <input type="hidden" id="coordinate" name="coordinate" value="${object.coordinate}">
            </div>
        </div>
    <#assign text=object.imgPath />
    <#assign data=text?eval />
        <div class="layui-form-item " pane>
            <label class="layui-form-label" style="height: 180px!important;">商圈封面图</label>
            <div class="layui-input-block" style="padding:  2px 2px 2px 15px;height: 180px!important;">
                <button type="button" class="layui-btn" id="test1">上传图片</button>
                <div class="layui-input-block">
                    <img class="layui-upload-img" id="demo1" style="margin: 4px -108px;" src="${data.maxPath}">
                    <p id="demoText" style="margin: 4px -108px;"></p>
                </div>
                <input type="hidden" name="imgPath" value="" id="imgPath">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">描述</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入内容(最长150字)" name="description" class="layui-textarea" maxlength="150">${object.description}</textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="formDemo">立即提交</button>
                <button type="button" class="layui-btn layui-btn-danger " onclick="javascript :history.go(-1);">返&nbsp;回</button>
            </div>
        </div>
        <input type="hidden" id="county_id" value="${object.county_id}" name="county_id">
    </form>
</div>

</body>
<script type="text/javascript" src="/static/common/js/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="/static/common/layui/layui.js"></script>
<script type="text/javascript">
    $("#imgPath").val('${object.imgPath}');
    // 百度地图API功能
    var map = new BMap.Map("allmap");
    var point = new BMap.Point(${object.coordinate});
    map.centerAndZoom(point, 11);

    function theLocation() {
        var parent_id = $('select[name="parent_id"]').val();
        if (parent_id == null && parent_id.trim() == '' && parent_id == undefined) {
            layer.alert("请先选择所属区域!");
            return;
        }
        var pname = $('#parent_id  option:selected').text().trim();
        var city = document.getElementById("cityName").value;
        if (city != "") {
            map.centerAndZoom("重庆市" + pname + city, 15);      // 用城市名设置地图中心点
        }
        $("#cityName-coordinate").val(city);
        $("#coordinate").val('');
        $("#coordinateShow").html('');
    }

    //在配置项中重新设定地图中心点
    function resetMapCenter() {
        var point = map.getCenter();
        if (point != null && point.lng != '106.558434' && point.lng != 0) {
            $("#coordinateShow").html(point.lng + "," + point.lat);
            $("#coordinate").val(point.lng + "," + point.lat);
        } else {
            $("#coordinate").val('');
            layer.alert("请正确输入区县名称!");
        }

    }
</script>

<script type="text/javascript">
    layui.use('form', function () {
        var form = layui.form;
        //监听提交
        form.on('submit(formDemo)', function (data) {
            var imgPath = $("#imgPath").val();
            var coordinate = $("#coordinate").val();
            if (coordinate == null || coordinate == '' || coordinate == undefined) {
                layer.alert("你还没有获取坐标!");
                return false;
            }
            if (imgPath == null || imgPath == '' || imgPath == undefined) {
                layer.alert("还没有上传封面图!");
                return false;
            }
            $('input[name="name"]').val('');
            $.post("/basics/tradeArea/updata.do", data.field, function (result, status) {
                if (status == "success") {
                    layer.msg('修改成功!', {icon: 1});
                    setTimeout('window.location="/basics/tradeArea/list.do"', 2000)
                } else {
                    layer.msg('修改失败!', {icon: 2});
                }
            }, "json");
            return false;
        });
    });
    layui.use('upload', function () {
        var $ = layui.jquery
                , upload = layui.upload;
        //普通图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            , url: '/basics/tradeArea/upload.do'
            , before: function (obj) {
                layer.msg('上传中...', {icon: 16, shade: 0.01});
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //如果上传失败
                if (res.success == false) {
                    layer.closeAll('loading');
                    return layer.alert('上传失败');
                } else { //上传成功
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #01b84e;">上传成功</span>');
                    $("#imgPath").val(JSON.stringify(res.data));
                    layer.closeAll('loading');
                    return layer.alert('上传成功');
                }
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });
    })
</script>
<script src="/static/common/js/ajaxhook.js" type="text/javascript"></script>
<script src="/static/common/js/ajaxHookData.js" type="text/javascript"></script>
</html>